﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.ComplexClasses;
using DAL;
using App_Log;
using App_Utils;
namespace BLL
{
    public class BL_Product
    {
        public static Product SaveRecord(Product objProduct)
        {
            try
            {
                clsLog.logMsg("Product", "SaveRecord", "Started");
                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblProducts where q.product_id == objProduct.product_id select q).SingleOrDefault();

                if (query != null)
                {
                    query = (tblProduct)clsUtils.ConvertToObject(objProduct, new tblProduct());
                    db.SaveChanges();
                }
                else
                {
                    tblProduct _objtblProduct = new tblProduct();
                    _objtblProduct = (tblProduct)clsUtils.ConvertToObject(objProduct, new tblProduct());
                    db.tblProducts.Add(_objtblProduct);
                    db.SaveChanges();
                    objProduct = (Product)clsUtils.ConvertToObject(_objtblProduct, objProduct);
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("Product", "SaveRecord", ex.Message);
            }
            return objProduct;
        }
    }
}
